package com.kob.backend.service.impl.user.bot;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.mapper.BotMapper;
import com.kob.backend.pojo.Bot;
import com.kob.backend.pojo.User;
import com.kob.backend.service.impl.utils.UserDetailsImpl;
import com.kob.backend.service.user.bot.BotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BotServiceImpl implements BotService {
    @Autowired
    private BotMapper botMapper;


    @Override
    public Map<String, String> addBot(Map<String, String> data) {
        UsernamePasswordAuthenticationToken authentication =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
        User user = loginUser.getUser();
        Map<String,String> map = new HashMap<>();
        String title = data.get("title");
        String code = data.get("code");
        String description = data.get("description");
        if(title != null && title.length()>100){
            map.put("error_message","标题长度不能大于100");
            return map;
        }
        if(code != null && code.length() > 10000){
            map.put("error_message","代码过长");
            return map;
        }
        if(description != null && description.length() > 300){
            map.put("error_message","描述不能超过300字");
            return map;
        }
        if(description == null || description.length() == 0){
            description = "这家伙很懒，什么也没留下";
        }
        Bot bot = new Bot();
        Date now = new Date();
        bot.setUserId(user.getId());
        bot.setTitle(title);
        bot.setDescription(description);
        bot.setCode(code);
        bot.setCreateTime(now);
        bot.setUpdateTime(now);
        botMapper.insert(bot);
        map.put("error_message","success");
        return map;
    }

    @Override
    public Map<String, String> deleteBot(Map<String, String> data) {
        Map<String, String> map = new HashMap<>();
        int id =Integer.parseInt(data.get("id"));
        botMapper.deleteById(id);
        map.put("error_message","success");
        return map;
    }

    @Override
    public List<Bot> selectBot() {
        UsernamePasswordAuthenticationToken authentication =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
        User user = loginUser.getUser();
        QueryWrapper<Bot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());
        return botMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, String> updateBot(Map<String, String> data) {
        int id = Integer.parseInt(data.get("id"));
        Map<String, String> map = new HashMap<>();
        String title = data.get("title");
        String code = data.get("code");
        String description = data.get("description");
        if(title != null && title.length()>100){
            map.put("error_message","标题长度不能大于100");
            return map;
        }
        if(code != null && code.length() > 10000){
            map.put("error_message","代码过长");
            return map;
        }
        if(description != null && description.length() > 300){
            map.put("error_message","描述不能超过300字");
            return map;
        }
        if(description == null || description.length() == 0){
            description = "这家伙很懒，什么也没留下";
        }
        Bot bot = new Bot();
        bot.setId(id);
        bot.setDescription(description);
        bot.setCode(code);
        bot.setTitle(title);
        Date now = new Date();
        bot.setUpdateTime(now);
        botMapper.updateById(bot);
        return map;
    }

    @Override
    public Bot selectOneBot(Map<String, String> data) {
        int id = Integer.parseInt(data.get("id"));
        return botMapper.selectById(id);
    }
}
